﻿var DialogHelper = new Object();
    DialogHelper.initializeDialog   = _initializeDialog;
    DialogHelper.resizeDialog       = _resizeDialog;
    DialogHelper.openDialog         = _openDialog;
    DialogHelper.closeDialog        = _closeDialog;
    DialogHelper.setTitle           = _setTitle;

/* initializeDialog */  
function _initializeDialog(dialogId, options)
{
    var autoOpen        = (options.autoOpen ? options.autoOpen : false);
    var closeOnEscape   = (options.closeOnEscape ? options.closeOnEscape : true);
    var modal           = (options.modal ? options.modal : true);
    var resizable       = (options.resizable ? options.resizable : false);
    var draggable       = (options.draggable ? options.draggable : true);
    var buttons         = (options.buttons ? options.buttons: {} );
    var width           = (options.width ? options.width : 400);
    var height          = (options.height ? options.height : 200);
    var open            = (options.open ? options.open: function(event, ui) {} );
    var close           = (options.close ? options.close: function(event, ui) { } );
    var preventClose    = options.preventClose;

    if (preventClose == true)
    {
        open = function(event, ui) { $(".ui-dialog-titlebar-close", ui.dialog | ui).hide(); };
        closeOnEscape = false;
        modal = true;
    }

    $("#" + dialogId).dialog
    (
        {
            autoOpen:       autoOpen,
            closeOnEscape:  closeOnEscape,
            modal:          modal,
            resizable:      resizable,
            draggable:      draggable,
            title:          options.title,
            zIndex:         900,
            width:          width,
            height:         height,
            open:           open,
            close:          close,
            buttons:        buttons
        }
    );
}

/* ResizeDialog */
function _resizeDialog(dialogId, width, height)
{
    //adjust size..
    if (width > 0)
    {
        $("#" + dialogId).dialog('option', 'width', width);
    }
    if (height > 0)
    {
        $("#" +dialogId).dialog('option', 'height', height);
    }
    //adjust position...
    $("#" +dialogId).dialog("option", "position", $("#" +dialogId).dialog("option", "position"));
}

/* Open dialog */
function _openDialog(dialogId, title)
{
    if ( !isEmpty(title) )
        $("#" + dialogId).dialog( { title: title } );

    $("#" + dialogId).dialog("open");
}

/* Close dialog */
function _closeDialog(dialogId)
{
    $("#" + dialogId).dialog("close");
}

/* Set title */
function _setTitle(dialogId, title)
{
    $("#" + dialogId).dialog("option", "title", title.toUpperCase());
}

function _preventFromClosing() {
    
}